package com.zxy.demo.guava;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * PredicateDemo.
 * 
 * Created by zxybird on 17/2/9.
 */
public class PredicateDemo {

  public static void main(String[] args) {
    Optional<ImmutableMultiset<Person>> optional = Optional.fromNullable(testPredict());

    if (optional.isPresent()) {
      for (Person p : optional.get()) {
        System.out.println("女人：" + p);
      }
    }

    System.out.println(optional.isPresent());
  }

  public static ImmutableMultiset<Person> testPredict() {
    List<Person> personList = Lists.newArrayList(new Person(1, 0, "a", "46546", 1, 20),
        new Person(2, 1, "ab", "46546", 0, 30), new Person(3, 1, "abc", "46546", 0, 25),
        new Person(4, 1, "aef", "46546", 1, 50), new Person(5, 1, "ade", "46546", 0, 27),
        new Person(6, 1, "acc", "46546", 1, 29), new Person(7, 1, "add", "46546", 0, 33));

    return ImmutableMultiset.copyOf(Collections2.filter(personList, new Predicate<Person>() {
      @Override
      public boolean apply(Person input) {
        return input.getSex() == 0;
      }
    }));
  }

}

